//
//  HWFrame.m
//  JWC
//
//  Created by lanou on 16/4/11.
//  Copyright © 2016年 陈帅. All rights reserved.
//

#import "HWFrame.h"
#import "HWUserModel.h"

static CGFloat avatarMaxY = 50;
static CGFloat inset = 10;
static CGFloat toolBarHeight = 50;
static CGFloat textX = 14;
static CGFloat topCmtH = 20;
@implementation HWFrame

- (void)setModel:(HWRecommendModel *)model
{
    _model = model;
    CGFloat textW = SCREEN_WIDTH - 28;
    CGFloat textH = [model.text boundingRectWithSize:CGSizeMake(textW , MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:14]} context:nil].size.height;
    //最大的Y值
    CGFloat maxY = avatarMaxY + inset*2 + textH ;
    if ([model.type isEqualToString:@"video"])
    {
        maxY = [self setupFrameWithMaxY:maxY height:model.video.height width:model.video.width model:model];
    }
    else if ([model.type isEqualToString:@"image"])
    {
        maxY = [self setupFrameWithMaxY:maxY height:model.image.height width:model.image.width model:model];
    }
    else if ([model.type isEqualToString:@"gif"])
    {
        maxY = [self setupFrameWithMaxY:maxY height:model.gif.height width:model.gif.width model:model];
    }
    else if ([model.type isEqualToString:@"audio"])
    {
        maxY = [self setupFrameWithMaxY:maxY height:model.audio.height width:model.audio.width model:model];
    }
    
    //如果有热门评论
    if(model.top_comment){
        
        NSString *content = [NSString stringWithFormat:@"%@ : %@", model.top_comment.u.name, model.top_comment.content];
        CGFloat topcmtContentH = [content boundingRectWithSize:CGSizeMake(textW, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:13]} context:nil].size.height;
        
        maxY = topcmtContentH + topCmtH + maxY + inset;
        
    }
    //设置cell的高度
    self.cellHeight = maxY + toolBarHeight;
}

- (CGFloat)setupFrameWithMaxY:(CGFloat)maxY height:(NSInteger)height width:(NSInteger)width model:(HWRecommendModel *)model
{
    CGFloat contentViewX = textX;
    CGFloat contentViewY = maxY;
    CGFloat contentViewW = SCREEN_WIDTH - 28;
    CGFloat widthScale = width / contentViewW;
    CGFloat contentViewH = height / widthScale;
    if ([model.type isEqualToString:@"video"] && contentViewH > 250) {
        contentViewH = 250;
    }if (contentViewH > 1500) {
        contentViewH = 300;
    }
    self.contentViewFrame = CGRectMake(contentViewX, contentViewY, contentViewW, contentViewH);
    
    return contentViewY + contentViewH + inset;
}

@end
